****************************************************************
* Setup for Suspension Data from SIMS Files 2004-2018          *
* Input: all .sav in "$saves\"                                 *
* Output: .dta file in "$saves\SSDR\sims_susp_2004-2018.dta"   *
* Jonas Zhonghan Xie                                           *
* Feburary 2024                                                *
****************************************************************

** Read in cleaned SIMS datasets from saves folder

capture program drop uniqueexp
program define uniqueexp
	syntax varlist,by(varlist min=1)
	foreach var of varlist `varlist'{
		cap drop obs obs_unique `var'_sum
		bysort `by' `var': gen obs = _n
		gen obs_unique = obs == 1
		bysort `by': egen `var'_sum = sum(obs_unique)
		drop obs obs_unique
	}
end

use "$saves\end04.dta",replace
forvalues y=4/9{
	append using "$saves\end0`y'.dta"
}
forvalues y=10/12{
	append using "$saves\end`y'.dta"
}

keep sasid year school inschsusp outschsusp
order sasid year *
format sasid %10.0f
sort sasid year


** This is to check the conflicting values of suspension in duplicates
uniqueexp outschsusp inschsusp, by(sasid school year)
tab outschsusp_sum
tab inschsusp_sum

drop if outschsusp_sum >1 | inschsusp_sum>1
drop outschsusp_sum inschsusp_sum

bys sasid year school: gen num = _n
keep if num == 1
drop num

** Aggregate the total number of days of suspension on year-sasid level
foreach var of varlist outschsusp inschsusp{
	bys sasid year: egen `var'_sum = sum(`var')
}

bys sasid year: gen num = _n
keep if num == 1
drop num outschsusp inschsusp
gen susp_sum = outschsusp_sum + inschsusp_sum

save "$saves\SSDR\sims_susp_2004-2012.dta",replace




